package com.evildog.workspace.file.service.impl;

import com.evildog.workspace.file.bo.VirtualFileBO;
import com.evildog.workspace.file.convert.VirtualFileConvert;
import com.evildog.workspace.file.service.UploadService;
import com.evildog.workspace.file.upload.decorator.DbFileUploadDecorator;
import com.evildog.workspace.file.upload.uploader.FileUploader;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
@Slf4j
public class UploadServiceImpl implements UploadService {

    @Autowired
    private DbFileUploadDecorator dbFileUploadDecorator;

    @Autowired
    @Qualifier("LocalFileUploader")
    private FileUploader fileUploader;

    @Override
    public VirtualFileBO upload(MultipartFile file, String folder, boolean save) {
        if (save) {
            return VirtualFileConvert.INSTANCE.convert(dbFileUploadDecorator.upload(file, folder));
        } else {
            return VirtualFileConvert.INSTANCE.convert(fileUploader.upload(file, folder));
        }
    }
}
